<template>
  <div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="btnAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
      >展开/折叠</el-button>
    </div>

    <el-table
        v-if="refreshTable"
        :default-expand-all="isExpandAll"
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        size="medium"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="菜单名称" width="160"></el-table-column>
      <el-table-column label="图标" >
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 18px"/>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" width="80"></el-table-column>
      <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="statu" label="状态">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small" v-if="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="type"
          label="类型">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 1" type="success">菜单</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 2" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" @click="editHandle(scope.row.id)">修改</el-button>
          <el-divider direction="vertical"></el-divider>
          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
              <el-button type="text" slot="reference">删除</el-button>
            </el-popconfirm>
        </template>
        </template>
      </el-table-column>
    </el-table>

<!--    新增对话框-->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="680px" :before-close="handleClose">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="editForm.parentId" placeholder="请选择上级菜单" style="width: 100%">
            <template v-for="item in menuOptions">
              <el-option :label="item.name" :value="item.id"></el-option>
              <template v-for="child in item.children">
                <el-option :label="child.name" :value="child.id">
                  <span>{{ "- " + child.name }}</span>
                </el-option>
                <template v-for="childTo in child.children">
                  <el-option :label="childTo.name" :value="childTo.id">
                    <span>{{ "-- " + childTo.name }}</span>
                  </el-option>
                </template>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限编码" prop="perms" label-width="100px">
          <el-input v-model="editForm.perms" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <template>
            <el-select v-model="editForm.icon" placeholder="请选择" style="width: 100%">
              <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.value">
                <i :class="item.value"/>：{{item.name}}
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="菜单URL" prop="path" label-width="100px">
          <el-input v-model="editForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="component" label-width="100px">
          <el-input v-model="editForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type" label-width="100px">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序号" prop="orderNum" label-width="100px">
          <el-input-number v-model="editForm.orderNum" :min="1" label="排序号">1</el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">{{ btnName }}</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "Menu",
  data() {
    return {
      dialogVisible: false,
      editForm:{},
      tableData: [],
      editFormRules: {
        parentId: [
          {required: true, message: '请选择上级菜单', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ],
        perms: [
          {required: true, message: '请输入权限编码', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        orderNum: [
          {required: true, message: '请填入排序号', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
      options: {},
      menuOptions: [],
      title:"",
      btnName:"",
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
    }
  },
  created() {
    this.getMenuTree()
  },
  methods: {
    getMenuTree() {
      this.request.get("/sys/menu/list").then(res => {
        this.tableData = res.data.data
      })
    },
    btnAdd(){
      this.dialogVisible = true
      //请求图标
      this.request.get("/sys/menu/icons").then(res => {
        this.options = res.data.data
      })
      this.menuOptions = [];
      const menu = { id: 0, name: '主类目', children: [] };
      menu.children = this.tableData
      this.menuOptions.push(menu)
      this.title = "添加菜单";
      this.btnName = "立即创建";
    },
    submitForm() {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.request.post('/sys/menu/' + (this.editForm.id?'update' : 'save'), this.editForm)
              .then(res => {
                this.$message({
                  showClose: true,
                  message: '恭喜你，操作成功',
                  type: 'success',
                  onClose:() => {
                    this.getMenuTree()
                  }
                });
                this.dialogVisible = false
              })
          this.menuOptions = []
          this.editForm = []
          this.$emit("refreshUser")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    editHandle(id) {
      this.request.get('/sys/menu/info/' + id).then(res => {
        this.editForm = res.data.data
      })
      //请求图标
      this.request.get("/sys/menu/icons").then(res => {
        this.options = res.data.data
      })
      this.title = "修改菜单";
      this.btnName = "立即修改";
      this.dialogVisible = true
    },
    resetForm() {
      this.$refs['editForm'].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    delHandle(id) {
      this.request.post("/sys/menu/delete/" + id).then(res => {
        this.$message({
          showClose: true,
          message: '恭喜你，操作成功',
          type: 'success',
          onClose:() => {
            this.getMenuTree()
          }
        });
      })
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
  }
}
</script>

<style scoped>

</style>